<template>
  <el-card>
    <template #header>
      <h3>商品列表</h3>
    </template>

    <div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }"
            ><ul>
              <li>
                <span>商品 ID</span>
                <p>{{ row.id }}</p>
              </li>
              <li>
                <span>商品名称</span>
                <p>{{ row.name }}</p>
              </li>
              <li>
                <span>所属分类</span>
                <p>{{ row.category }}</p>
              </li>
              <li>
                <span>商品价格</span>
                <p>{{ row.price }}</p>
              </li>
              <li>
                <span>创建时间</span>
                <p>{{ row.ctime | formateDate }}</p>
              </li>
              <li>
                <span>商品销量</span>
                <p>{{ row.sellCount }}</p>
              </li>
              <li>
                <span>商品评价</span>
                <p>{{ row.rating }}</p>
              </li>
              <li>
                <span>商品描述</span>
                <p>{{ row.goodsDesc }}</p>
              </li>
            </ul></template
          >
        </el-table-column>
        <el-table-column label="商品名称" prop="name" width="270">
        </el-table-column>
        <el-table-column label="商品分类" prop="category" width="140">
        </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片" width="180">
          <template v-slot="{ row }">
            <img :src="BasePicUrl + row.imgUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="goodsDesc" width="380">
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="143">
          <template v-slot="{ row }">
            <el-button size="mini" type="primary" @click="editRow(row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="delGoods(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 编辑页面 -->
      <el-dialog title="编辑商品" :visible.sync="dialogVisible" width="50%">
        <div>
          <!-- 推荐使用表单的形式修改 -->
          {{ editCurData }}
          <el-input v-model="editCurData.rating"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini"
            >取 消</el-button
          >
          <el-button type="primary" size="mini" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { goodsList, delGoods } from '@/api/goods.js'
import moment from 'moment'
import _ from 'lodash'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      editCurData: {},
      BasePicUrl: 'http://127.0.0.1:5000/upload/imgs/goods_img/'
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data, total } = await goodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // console.log(data)
      this.tableData = data
      this.total = total
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getGoodsList()
    },
    delGoods(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await delGoods({ id })
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            this.getGoodsList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editRow(o) {
      this.editCurData = _.cloneDeep(o)
      this.dialogVisible = true
    }
  },
  filters: {
    formateDate(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-pagination {
  margin-top: 20px;
}
img {
  width: 60px;
}
ul {
  width: 700px;
  display: flex;
  flex-wrap: wrap;
  li {
    width: 50%;
    display: flex;
    margin-bottom: 10px;
    span {
      width: 100px;
    }
  }
}
</style>
